package com.le.edu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.le.edu.domain.entity.Course;
import com.le.edu.domain.entity.Teacher;
import com.le.edu.domain.vo.TeacherAndCourseListVO;
import com.le.edu.domain.vo.TeacherVO;
import com.le.edu.exception.AppException;
import com.le.edu.mapper.CourseMapper;
import com.le.edu.mapper.TeacherMapper;
import com.le.edu.service.TeacherService;
import com.le.edu.utils.PasswordEncoder;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author 17447
 * @description 针对表【t_teacher(讲师)】的数据库操作Service实现
 * @createDate 2024-01-07 20:07:46
 */
@Service
@RequiredArgsConstructor
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
        implements TeacherService {
    private final ThreadPoolExecutor executor;
    private final CourseMapper courseMapper;

    @Override
    public Page<TeacherVO> pageTeacherList(Page<TeacherVO> page, LambdaQueryWrapper<Teacher> wrapper) {
        return baseMapper.selectTeacherList(page, wrapper);
    }

    @Override
    public int resetPassword(Teacher teacher) {
        teacher.setPassword(PasswordEncoder.encrypt(teacher.getPassword()));
        return baseMapper.updateById(teacher);
    }

    @Override
    public void checkUsernameUniqueNeTeacherId(Long teacherId, String name) {
        Long count = baseMapper.selectCount(Wrappers.<Teacher>lambdaQuery()
                .ne(Teacher::getTeacherId, teacherId)
                .eq(Teacher::getName, name));
        if (count > 0) {
            throw new AppException("用户名已存在");
        }
    }

    @Override
    public void checkUsernameUnique(String name) {
        Long count = baseMapper.selectCount(Wrappers.<Teacher>lambdaQuery()
                .eq(Teacher::getName, name));
        if (count > 0) {
            throw new AppException("用户名已存在");
        }
    }

    @Override
    public List<Teacher> listNameAndId() {
        return baseMapper.selectTeacherListNameAndId(new LambdaQueryWrapper<Teacher>()
                .eq(Teacher::getDelFlag, "0"));
    }

    @Override
    public TeacherAndCourseListVO getTeacherAndCourse(Long teacherId) {
        TeacherAndCourseListVO vo = new TeacherAndCourseListVO();

        CompletableFuture<Void> teacherFuture = CompletableFuture.runAsync(() -> {
            Teacher teacher = baseMapper.selectById(teacherId);
            BeanUtil.copyProperties(teacher, vo);
        }, executor);

        CompletableFuture<Void> courseFuture = CompletableFuture.runAsync(() -> {
            List<Course> courses = courseMapper.selectList(new LambdaQueryWrapper<Course>()
                    .eq(Course::getTeacherId, teacherId));
            vo.setCourseList(courses);
            vo.setTotalCourse(courses.size());
        }, executor);

        CompletableFuture.allOf(teacherFuture, courseFuture).join();
        return vo;
    }
}




